#include <iostream>
#include "..//Z80Core/z80.h"
#include "GenTables.h"
#include "CheckTables.h"

using namespace std;

void CheckSZP()
{
	unsigned short int i = 0;
	for ( ; i <= 255 ; ++i )
	{
		if (CalcSZP((byte)i) != PZSTable[i])
		{
			cout << "err @ " << (short int)i << endl;
		}
	}
}
void CheckSZ()
{
	unsigned short int i = 0;
	for ( ; i <= 255 ; ++i )
	{
		if (CalcSZ((byte)i) != ZSTable[i])
		{
			cout << "err @ " << (short int)i << endl;
		}
	}
}
int CheckDAA2 (byte iA, byte iF, byte oA, byte oF)
{
	word index, v;
	byte tA,tF;
	index = iA;

	if(iF&C_FLAG) index|=256;
	if(iF&BIT2) index|=512;
	if(iF&N_FLAG) index|=1024;

	v=DAATable[index];
	
	tF = (v&0xFF);
	tA = ((v&0xFF00)>>8);
	
	if ((oF == tF) && (oA ==tA)) return 1;

	return 0;
}
int CheckDAA(byte iA, byte iF, byte oA, byte oF)
{
	byte hf, nf, cf, low, hi, diff;
	hf = (iF & BIT2);
	nf = (iF & N_FLAG);
	cf = (iF & C_FLAG);
	low = (iA & 0x0F);
	hi = (iA / 16);
	byte tA, tF;

	oF &= 0xD7;

	if (cf)
	{
		if (low <= 9 && !hf) diff = 0x60;
		else diff = 0x66;
	}
	else
	{
		if (low >= 10)
		{
			if (hi <=8) diff = 0x06;
			else diff = 0x66;
		}
		else
		{
			if (hi >= 10)
			{
				if (hf) diff = 0x66;
				else diff = 0x60;
			}
			else
			{
				if (hf) diff = 0x06;
				else diff = 0;
			}
		}
	}
	if (nf) tA = iA - diff;
	else tA = iA + diff;

	tF = CalcSZP(tA) | (iF & N_FLAG);
	if (cf || (low <= 9 ? hi >= 10 : hi >= 9)) tF |= C_FLAG;
	if (nf ? hf && low <= 5 : low >= 10)  tF |= H_FLAG;

	if ((oF == tF)&&(oA == tA)) return 1;

	return 0;
}